package com.yanqu.road.server.cmd.activity.xingyoushanshui;

import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shanshui.XingYouShanShuiModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.shanshui.XingYouShanShuiMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

@CmdApi(reqMsg = XingYouShanShuiProto.ShanShuiEnterMapReq.class, respMsg = XingYouShanShuiProto.ShanShuiEnterMapRsp.class)
@Cmd(code = Protocol.S_XING_YOU_SHAN_SHUI_ENTER_MAP, desc = "进入地图")
public class ShanShuiEnterMapCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        XingYouConfig config = XingYouShanShuiMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        //休息期判断
        long todayZeroTimeStamp = DateHelper.getTodayZeroTimeStamp();
        long nowTimes = System.currentTimeMillis();
        if(nowTimes < todayZeroTimeStamp + config.getSHANSHUI_FIGHT_TIME().get(0) * DateHelper.MINUTE_MILLIONS ||
                nowTimes > todayZeroTimeStamp + config.getSHANSHUI_FIGHT_TIME().get(1) * DateHelper.MINUTE_MILLIONS){
            ret = GameErrorCode.E_XING_YOU_SHAN_SHUI_IN_REST_TIME;
            return;
        }

        XingYouShanShuiProto.ShanShuiEnterMapReq req = XingYouShanShuiProto.ShanShuiEnterMapReq.parseFrom(packet.getBytes());
        ret = player.getModule(XingYouShanShuiModule.class).enterMap(req);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_XING_YOU_SHAN_SHUI_ENTER_MAP, XingYouShanShuiProto.ShanShuiEnterMapRsp.newBuilder().setRet(ret));
        }
    }
}
